<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="true"
      label-width="80px"
      @submit.native.prevent
    >
      <el-form-item label="库龄范围" prop="inventoryAgeDaysMore">
        <el-input
          v-model="queryParams.inventoryAgeDaysMore"
          placeholder="最低库龄"
          style="width: 100px"
        />&nbsp;天-<el-input
          v-model="queryParams.inventoryAgeDaysLess"
          placeholder="最高库龄"
          style="width: 100px"
        />&nbsp;天
      </el-form-item>
      <el-form-item label="库存量" prop="inventoryAgeDateRange">
        <el-input
          v-model="queryParams.inventoryQuantityMore"
          placeholder="最低库存量"
          style="width: 100px"
        />-<el-input
          v-model="queryParams.inventoryQuantityLess"
          placeholder="最高库存量"
          style="width: 100px"
        />
      </el-form-item>
      <!-- <el-form-item label="" prop="productSeason">
        <el-select
          v-model="queryParams.productSeason"
          placeholder="请选择商品-季节"
          clearable
          style="width: 150px"
        >
          <el-option
            v-for="dict in dict.type.sys_product_jijie"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="" prop="firstLevelColumn">
        <el-select
          v-model="queryParams.firstLevelColumn"
          placeholder="请选择一级类目"
          clearable
          style="width: 150px"
          @change="getsecondaryList"
        >
          <el-option
            v-for="dict in dict.type.sys_product_pl"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="" prop="secondLevelColumn">
        <el-select
          v-model="queryParams.secondLevelColumn"
          placeholder="请选择二级类目"
          clearable
          filterable
          @change="getthreeLevelColumnList"
        >
          <el-option
            v-for="dict in secondaryList"
            :key="dict"
            :label="dict"
            :value="dict"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="" prop="threeLevelColumn">
        <el-select
          v-model="queryParams.threeLevelColumn"
          placeholder="请选择三级类目"
          clearable
          filterable
          style="width: 150px"
        >
          <el-option
            v-for="dict in threeLevelColumnList"
            :key="dict"
            :label="dict"
            :value="dict"
          />
        </el-select>
      </el-form-item> 
      <el-form-item label="产品款号" prop="item">
        <el-select
          v-model="queryParams.item"
          placeholder="产品款号"
          clearable
          style="width: 150px"
          filterable
          remote
          :remote-method="getDatas"
        >
          <el-option
            v-for="item in options"
            :key="item"
            :label="item"
            :value="item"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="sku" prop="sku">
        <el-select
          v-model="queryParams.sku"
          placeholder="请选择商品-sku"
          filterable
          clearable
          allow-create
          remote
          :remote-method="getselectSkuLikeList"
        >
          <el-option
            v-for="(dict, index) in selectSkuLikeList"
            :key="index"
            :label="dict"
            :value="dict"
          />
        </el-select>
      </el-form-item>-->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['financial:BiInventoryAgeDwd:export']"
          >导出</el-button
        >
        <el-button
          :icon="!isOpen ? 'el-icon-arrow-down' : 'el-icon-arrow-up'"
          size="mini"
          @click="shouqi"
          >展开/收起</el-button
        >
      </el-form-item>
      <div v-if="isOpen">
        <el-form-item label="" prop="brandName">
          <!-- <el-input
          v-model="queryParams.brand"
          placeholder="请输入品牌"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
          <el-select
            v-model="queryParams.brand"
            placeholder="选择品牌"
            clearable
            filterable
          >
            <el-option
              v-for="dict in dict.type.sys_product_pp"
              :key="dict.value"
              :label="dict.label"
              :value="dict.label"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="" prop="productSeason">
          <el-select
            allow-create
            v-model="queryParams.productSeason"
            placeholder="请选择商品-季节"
            clearable
            style="width: 150px"
          >
            <el-option
              v-for="dict in dict.type.sys_product_jijie"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="" prop="firstLevelColumn">
          <el-select
            allow-create
            v-model="queryParams.firstLevelColumn"
            placeholder="请选择一级类目"
            clearable
            style="width: 150px"
            @change="getsecondaryList"
          >
            <el-option
              v-for="dict in dict.type.sys_product_pl"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="" prop="secondLevelColumn">
          <el-select
            allow-create
            v-model="queryParams.secondLevelColumn"
            placeholder="请选择二级类目"
            clearable
            filterable
            @change="getthreeLevelColumnList"
          >
            <el-option
              v-for="dict in secondaryList"
              :key="dict"
              :label="dict"
              :value="dict"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="" prop="threeLevelColumn">
          <el-select
            allow-create
            v-model="queryParams.threeLevelColumn"
            placeholder="请选择三级类目"
            clearable
            filterable
            style="width: 150px"
          >
            <el-option
              v-for="dict in threeLevelColumnList"
              :key="dict"
              :label="dict"
              :value="dict"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="产品款号" prop="item">
          <el-select
            allow-create
            v-model="queryParams.item"
            placeholder="产品款号"
            clearable
            style="width: 150px"
            filterable
            remote
            :remote-method="getDatas"
          >
            <el-option
              v-for="item in options"
              :key="item"
              :label="item"
              :value="item"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="sku" prop="sku">
          <el-select
            allow-create
            v-model="queryParams.sku"
            placeholder="请选择商品-sku"
            filterable
            clearable
            remote
            :remote-method="getselectSkuLikeList"
          >
            <el-option
              v-for="(dict, index) in selectSkuLikeList"
              :key="index"
              :label="dict"
              :value="dict"
            />
          </el-select>
        </el-form-item>
      </div>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['financial:BiInventoryAgeDwd:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['financial:BiInventoryAgeDwd:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['financial:BiInventoryAgeDwd:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['financial:BiInventoryAgeDwd:export']"
          >导出</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-refresh"
          size="mini"
          @click="handleRefresh"
          >同步</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row> -->

    <el-table
      :height="600"
      v-loading="loading"
      :data="BiInventoryAgeDwdList"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="库龄数据id" align="center" prop="id" />-->

      <el-table-column :label="'品牌'" align="center" prop="brand" />
      <el-table-column
        :label="'一级类目'"
        align="center"
        prop="firstLevelColumn"
      />
      <el-table-column
        width="140"
        :label="'二级类目'"
        align="center"
        prop="secondLevelColumn"
      />
      <el-table-column
        width="140"
        :label="'三级类目'"
        align="center"
        prop="threeLevelColumn"
      />
      <el-table-column label="产品季节" align="center" prop="productSeason" />
      <el-table-column label="产品款号(item)" align="center" prop="item" />
      <el-table-column label="产品款号(spu)" align="center" prop="spu" />
      <el-table-column label="sku" align="center" prop="sku" />
      <!-- <el-table-column label="入仓时间" align="center" prop="dataDate">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dataDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>-->
      <el-table-column
        label="库龄天数"
        align="center"
        prop="inventoryAgeDays"
      />
      <el-table-column
        label="归属库龄段"
        align="center"
        prop="inventoryAgeDateRange"
      />
      <el-table-column label="库存量" align="center" prop="inventoryQuantity" />
      <el-table-column label="库存金额" align="center" prop="inventoryAmount" />
      <!-- <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['financial:BiInventoryAgeDwd:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['financial:BiInventoryAgeDwd:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column> -->
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改库龄对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="sku" prop="sku">
          <el-input v-model="form.sku" placeholder="请输入sku" />
        </el-form-item>
        <el-form-item label="库龄天数" prop="inventoryAgeDays">
          <el-input
            v-model="form.inventoryAgeDays"
            placeholder="请输入库龄天数"
          />
        </el-form-item>
        <el-form-item label="归属库龄段" prop="inventoryAgeDateRange">
          <el-input
            v-model="form.inventoryAgeDateRange"
            placeholder="请输入归属库龄段"
          />
        </el-form-item>
        <el-form-item label="库存量" prop="inventoryQuantity">
          <el-input
            v-model="form.inventoryQuantity"
            placeholder="请输入库存量"
          />
        </el-form-item>
        <el-form-item label="库存金额" prop="inventoryAmount">
          <el-input
            v-model="form.inventoryAmount"
            placeholder="请输入库存金额"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listBiInventoryAgeDwd,
  getBiInventoryAgeDwd,
  delBiInventoryAgeDwd,
  addBiInventoryAgeDwd,
  updateBiInventoryAgeDwd,
  synData,
  voList,
  selectDeadGoodsList,
} from "@/api/Warehouse/BiInventoryAgeDwd";
import {
  itemSaleData,
  skcByItemSaleList,
  queryContentByKey,
  selectItemStatistics,
  skuBySkcSaleList,
  comparisonStatistics,
} from "@/api/sku/sku";
import {
  listTemplate,
  getTemplate,
  delTemplate,
  addTemplate,
  updateTemplate,
  addTemplateAndSecondry,
  addProNoColorSize,
  productNoLike,
  proFirstSecondThree,
  selectSkuLike,
} from "@/api/product/template";
export default {
  name: "BiInventoryAgeDwd",
  dicts: [
    "sys_product_age",
    "sys_product_item",
    "sys_product_secondary",
    "sys_product_cycle",
    "sys_product_pl",
    "sys_product_jijie",
    "sys_product_pp",
  ],
  data() {
    return {
      selectSkuLikeList: [],
      threeLevelColumnList: [],
      secondaryList: [],
      options: [], // 选项，从后端传递过来
      isOpen: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 库龄表格数据
      BiInventoryAgeDwdList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sku: null,
        inventoryAgeDays: null,
        inventoryAgeDateRange: null,
        inventoryQuantity: null,
        inventoryAmount: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        sku: [{ required: true, message: "sku不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //sku列表
    getselectSkuLikeList(key) {
      selectSkuLike({ value: key }).then((response) => {
        this.selectSkuLikeList = response;
      });
    },
    getthreeLevelColumnList(key) {
      proFirstSecondThree({
        firstLevelList: this.queryParams.firstLevelColumn,
        secondLevelList: this.queryParams.secondLevelColumn,
        label: "三级",
      }).then((response) => {
        this.threeLevelColumnList = response;
      });
    },
    getsecondaryList(key) {
      proFirstSecondThree({
        firstLevelList: this.queryParams.firstLevelColumn,
        label: "二级",
      }).then((response) => {
        this.secondaryList = response;
      });
    },
    /* 获取货号列表 */
    getDatas(key) {
      console.log(JSON.stringify(key));
      // this.loading = true;
      // return
      queryContentByKey({ keyWord: key }).then((res) => {
        // this.loading = false;
        this.options = res.data;
        console.log(JSON.stringify(this.options));
      });
    },
    //搜索栏展示与收起
    shouqi() {
      // console.log(1);
      this.isOpen = !this.isOpen;
    },
    // 打开加载层
    openLoading() {
      this.$modal.loading("正在加载数据，请稍候！");
    },
    /** 刷新缓存按钮操作 Warning*/
    handleRefresh() {
      // this.$modal.msgWarning("数据正在同步，请稍候");

      // const obj = {
      //   startTime: this.timeGap.startTime,
      //   endTime: this.timeGap.endTime,
      // };
      this.openLoading();
      synData().then(() => {
        // this.$modal.msgSuccess("同步数据成功");
        this.$modal.closeLoading();
        this.getList();
        // this.$store.dispatch('dict/cleanDict');
      });
    },
    /** 查询库龄列表 */
    getList() {
      this.loading = true;
      voList(this.queryParams).then((response) => {
        this.BiInventoryAgeDwdList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        sku: null,
        inventoryAgeDays: null,
        inventoryAgeDateRange: null,
        inventoryQuantity: null,
        inventoryAmount: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加库龄";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getBiInventoryAgeDwd(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改库龄";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateBiInventoryAgeDwd(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBiInventoryAgeDwd(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除库龄编号为"' + ids + '"的数据项？')
        .then(function () {
          return delBiInventoryAgeDwd(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "financial/BiInventoryAgeDwd/voExport",
        {
          ...this.queryParams,
        },
        `BiInventoryAgeDwd_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
